import entity.Instruction;
import entity.MainMemory;
import entity.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//第二题 测试先进先出页表调度的主函数入口
public class Test_FIFO {
    public static void main(String[] args) {
        Page[] page = new Page[7];
        //7页作业 用数组存储
        page[0] = new Page(0, 1, '5', 11, 0);
        page[1] = new Page(1, 1, '8', 12, 0);
        page[2] = new Page(2, 1, '9', 13, 0);
        page[3] = new Page(3, 1, '1', 21, 0);
        page[4] = new Page(4, 0, '*', 22, 0);
        page[5] = new Page(5, 0, '*', 23, 0);
        page[6] = new Page(6, 0, '*', 121, 0);
        MainMemory mm = new MainMemory();
        //内存初始化
        mm.init(page);
        //12条指令 数组存储
        List<Instruction> job = new ArrayList<>();
//        job.add(new Instruction( 1,0,70,1));
//        job.add(new Instruction( 2,1,50,1));
//        job.add(new Instruction( 3,2,15,1));
//        job.add(new Instruction( 4,3,21,1));
//        job.add(new Instruction( 5,0,56,0));
//        job.add(new Instruction( 6,6,40,1));
//        job.add(new Instruction( 7,4,53,1));
//        job.add(new Instruction( 8,5,23,1));
//        job.add(new Instruction( 9,1,37,1));
//        job.add(new Instruction(10,2,78,0));
//        job.add(new Instruction(11,4, 1,1));
//        job.add(new Instruction(12,6,84,0));

        //随机生成测试数据进行先进先出检验
        for (int i = 1; i <= 12; i++) {
            job.add(new Instruction(i, new Random().nextInt(7),
                    new Random().nextInt(128),new Random().nextInt(2)));
        }
        //输出初始化页表
        for (int p = 0; p <= 6; p++) {
            System.out.println(page[p]);
        }
        //主要执行语句代码
        for (int n = 0; n <= 11; n++) {
            Instruction cur=job.get(n);
            System.out.println(">>>" + cur);
            cur.work(mm.list);
            if (!cur.getIsInter()) {
                mm.FIFO(cur, page);//所需页面不在主存 FIFO算法
                cur.work(mm.list);//再次调用执行指令计算绝对地址
            }
        }
        //输出最终调度后的页表
        System.out.println();
        for (int p = 0; p <= 6; p++) {
            System.out.println(page[p]);
        }

    }

}